package com.dzsw.zk.seq;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

public class ZookeeperClient {

    public static CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder()
            .sessionTimeoutMs(4000)
            .connectionTimeoutMs(1000)
            .maxCloseWaitMs(5000)
            .connectString(Const.CONNECT_URL)
            .defaultData("0".getBytes())
            .retryPolicy(new ExponentialBackoffRetry(3000, 3))
            .threadFactory(new ThreadFactory() {
                public final AtomicInteger counter = new AtomicInteger(0);

                public Thread newThread(Runnable r) {
                    Thread thread = new Thread(r, "curator-" + counter.getAndIncrement());
                    thread.setDaemon(true);
                    return thread;
                }
            });

    public ZookeeperClient() {
    }

    public CuratorFrameworkFactory.Builder getZookeeperInstance() {
        return builder;
    }
}
